# PHP For Nginx
# Date: 2013-12-30

function php53() {
logs "Info" "Add System User \"www\""
user_add www
logs "Info" "Install PHP 5.3.28 For Nginx"
cd $CWD
logs "Info" "Decompressing php-${phpVer}.tar.gz"
tar -zxf php-${phpVer}.tar.gz
cd php-$phpVer
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--with-config-file-scan-dir=/usr/local/php/etc/php.d \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-iconv-dir=/usr/local \
--with-fpm-user=www \
--with-fpm-group=www \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-bz2 \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-magic-quotes \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--with-curlwrappers \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-mcrypt \
--enable-ftp \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--with-gettext
make ZEND_EXTRA_LIBS='-liconv' && make install
cp php.ini-production /usr/local/php/etc/php.ini
ln -s /usr/local/php/etc/php.ini /etc/php.ini
cd ..
mv /usr/local/php/etc/php-fpm.conf /usr/local/php/etc/php-fpm.conf.bak
cp $CWD/conf/php-fpm-5.3.conf /usr/local/php/etc/php-fpm.conf
ln -s /usr/local/php/etc/php-fpm.conf /etc/
if [ "$linux_type" = "rhel" ] ; then
   cp $CWD/conf/php-fpm.init /etc/init.d/php-fpm
   chmod 755 /etc/init.d/php-fpm
   chkconfig --add php-fpm >/dev/null 2>&1 && chkconfig --level 345 php-fpm on
   [ "$?" != "0" ] && echo "/etc/init.d/php-fpm start" >> /etc/rc.local
else
   cp $CWD/conf/php-fpm-init_for-all.sh /etc/init.d/php-fpm
   sed -i '/exit 0/i\/etc/init.d/php-fpm start' /etc/rc.local
fi
chmod +x /etc/init.d/php-fpm
}

function php52() {
cd $CWD
logs "Info" "Add System User \"www\""
user_add www
logs "Info" "Install PHP 5.2.17 For Nginx"
[ ! -f /usr/bin/patch ] && yum -y install patch
logs "Info" "Decompressing php-5.3.17.tar.gz"
tar zxf php-5.2.17.tar.gz
logs "Info" "Patching for php 5.3.17"
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1
cd php-5.2.17/
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-config-file-scan-dir=/usr/local/php/etc/php.d --with-fpm-pid=/usr/local/php/var/run/php-fpm.pid --with-fpm-log=/usr/local/php/var/log/php-fpm.log --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring=all --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --with-iconv-dir=/usr/local
make ZEND_EXTRA_LIBS='-liconv' && make install
cp php.ini-dist /usr/local/php/etc/php.ini
ln -s /usr/local/php/etc/php.ini /etc/php.ini
mv /usr/local/php/etc/php-fpm.conf /usr/local/php/etc/php-fpm.conf.bak
cp $CWD/conf/php-fpm-5.2.conf /usr/local/php/etc/php-fpm.conf
ln -s /usr/local/php/etc/php-fpm.conf /etc/
cd ..
echo "/usr/local/php/sbin/php-fpm start" >> /etc/rc.local
}